package com.itheima.网络编程.tcp编程.tcp编程.文件上传;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

public class ThreadCs implements Runnable {
    private Socket sk;

    public ThreadCs(Socket sk) {
        this.sk = sk;
    }

    @Override
    public void run() {

        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\Users\\身体和心灵\\Desktop\\图片\\" + UUID.randomUUID().toString() + ".txt"));
            BufferedInputStream bis = new BufferedInputStream(sk.getInputStream());
            byte[] bys = new byte[1024 * 1024];
            int len;
            while ((len = bis.read(bys)) != -1) {
                bos.write(bys, 0, len);
                bos.flush();
            }
            System.out.println("========================");
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(sk.getOutputStream()));
            System.out.println("服务器反馈:");
            bw.write("上传成功");
            bw.close();
            bos.close();
            bis.close();
            sk.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
